<?php
require 'core/header.php';
if (isset($_GET['forumid'])) {
    if (!isset($_GET['p'])) {
        $p = 1;
    } else {
        $p = $_GET['p'];
    }
    $getforumid = $_GET['forumid'];
} else {
    header("Location:forum.php");
}


echo '<hr>';
if (isset($_GET['type']))
    $type = $_GET['type'];
else
    $type = 0;
if (isset($_GET['forumid'])) {
    echo '<p>板块说明：</p>';
    $forums = sql_forumid_forums($_GET['forumid']);
    echo htmlspecialchars_decode($forums['forum_desc']);
    echo '<a name="post"></a>';
    echo '<br /><a id="post_topic" href="topicediter.php?mod=post&action=newtopic&fid=' . $_GET['forumid'] . '">发帖</a>';

    //主题从这里开始
    //翻页这里开始
    $count_result = sql_forumid_nomal_type_page($getforumid, $type);
    $count_array = mysqli_fetch_array($count_result);
    $top_count = mysqli_fetch_array(sql_count_forumid_toptopic($getforumid, $type));
    $pagenum = ceil(($count_array['COUNT(*)'] - $top_count['COUNT(*)']) / 10);
    echo '<p style="text-align: right">';
    if ($p > 1) {
        echo '<a href="forums.php?forumid=' . $getforumid . '&p=' . ($p - 1) . '#post"><<上一页</a>&nbsp;&nbsp;';
    }
    echo '共 ', $count_array['COUNT(*)'], ' 条';
    if ($pagenum > 1) {
        for ($i = 1; $i <= $pagenum; $i++) {
            if ($i == $p) {
                echo '&nbsp;[', $i, ']';
            } else {
                echo '&nbsp;<a href="forums.php?forumid=' . $getforumid . '&p=' . $i . '#post">' . $i . '</a>';
            }
        }
    }
    if ($p < $pagenum) {
        echo '&nbsp;&nbsp;<a href="forums.php?forumid=' . $getforumid . '&p=' . ($p + 1) . '#post">下一页>></a>';
    }
    echo '</p>';
//翻页这里结束
    if (isset($_GET['type'])) {
        $type = $_GET['type'];
        switch ($type) {
            case "0":
                {
                    ?>
                    [全部][<a
                        href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=1&p=1#post_topic">求助</a>][<a
                        href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=2&p=1#post_topic">已解决</a>]
                    <?php
                    break;
                }
            case "1":
                ?>
                [<a href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=0&p=1#post_topic">全部</a>][求助][<a
                    href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=2&p=1#post_topic">已解决</a>]
                <?php
                break;
            case "2":
                ?>
                [<a href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=0&p=1#post_topic">全部</a>][<a
                    href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=1&p=1#post_topic">求助</a>][已解决]
                <?php
                break;
        }
    } else {
        ?>
        [全部][<a
                href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=1&p=1#post_topic">求助</a>][<a
                href="forums.php?forumid=<?= $_GET['forumid'] ?>&type=2&p=1#post_topic">已解决</a>]
        <?php
    }
//
    echo '<table border="1" width="100%" cellpadding="0" cellspacing="0">';
    echo '<tr><th width="5%">主题ID</th><th width="30%">主题名</th><th width="20%">投稿时间</th><th width="10%">投稿者</th><th width="20%">回帖数</th></tr>';

    $search_topics_desc_top = sql_forumid_nomal_type_topics_top($_GET['forumid'], $type, ($p - 1) * 10, 10);
    while (($result_array = mysqli_fetch_array($search_topics_desc_top)) != false){
        echo '<tr>';
        echo '<td>' . $result_array['topic_id'] . '</td>';
        $type_val = sql_topicid_type($result_array['topic_id']);
        $fid = $_GET['forumid'];
        if ($type_val == 1) {
            $type_url = "<a href='forums.php?forumid=" . $fid . "&type=1&p=1'><span style=\"color: #FF0000\">求助</span></a>";
        } elseif ($type_val == 2) {
            $type_url = "<span href='forums.php?forumid=" . $fid . "&type=2&p=1'><span style=\"color: #00CC00\">已解决</span></a>";
        }
        echo '<td>[<span style="color:#FF6600">置顶</span>][' .$type_url.']<a href="topic.php?topicid=' . $result_array['topic_id'] . '&p=1">' . $result_array['topic_title'] . '</a></td>';
        echo '<td>' . date("Y-m-d H:i:s", $result_array['topic_createtime']) . '</td>';
        $users = sql_searchName_login_uid_name($result_array['topic_poster']);
        echo '<td><a href ="home.php?mod=space&uid=' . $result_array['topic_poster'] . '">' . $users . '</a></td>';
        echo '<td><center>' . $result_array['topic_replies'] . '<br />' . date("Y-m-d H:i:s", sql_time_final_ftid($result_array['topic_id'])) . '</center></td>';
        echo '</tr>';
    }

    $search_topics_desc = sql_forumid_nomal_type_topics($_GET['forumid'], $type, ($p - 1) * 10, 10);
    while (($result_array = mysqli_fetch_array($search_topics_desc)) != false) {
        echo '<tr>';
        echo '<td>' . $result_array['topic_id'] . '</td>';
        $type_val = sql_topicid_type($result_array['topic_id']);
        $fid = $_GET['forumid'];
        if ($type_val == 1) {
            $type_url = "<a href='forums.php?forumid=" . $fid . "&type=1&p=1'><span style=\"color: #FF0000\">求助</span></a>";
        } elseif ($type_val == 2) {
            $type_url = "<span href='forums.php?forumid=" . $fid . "&type=2&p=1'><span style=\"color: #00CC00\">已解决</span></a>";
        }
        echo '<td>[' .$type_url.']<a href="topic.php?topicid=' . $result_array['topic_id'] . '&p=1">' . $result_array['topic_title'] . '</a></td>';
        echo '<td>' . date("Y-m-d H:i:s", $result_array['topic_createtime']) . '</td>';
        $users = sql_searchName_login_uid_name($result_array['topic_poster']);
        echo '<td><a href ="home.php?mod=space&uid=' . $result_array['topic_poster'] . '">' . $users . '</a></td>';
        echo '<td><center>' . $result_array['topic_replies'] . '<br />' . date("Y-m-d H:i:s", sql_time_final_ftid($result_array['topic_id'])) . '</center></td>';
        echo '</tr>';
    }
    echo '</table>';
}

//主题这里结束


echo '<input id="forumid" type="hidden" val="' . $_GET['forumid'] . '"/><input id="type" type="hidden" val="add"/>';
echo '<a href="topicediter.php?mod=post&action=newtopic&fid=' . $_GET['forumid'] . '">发帖</a>';

require 'core/footer.php';